package com.example.bo;

import com.example.domain.CmsPermission;
import com.example.domain.CmsUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <h3>duty-test1</h3>
 * <p></p>
 *
 * @author : 你的名字
 * @date : 2020-05-29 10:49
 **/
public class AdminUserDetails implements UserDetails {

    private CmsUser cmsUser;
    private List<CmsPermission> permissions;

    public AdminUserDetails(CmsUser cmsUser, List<CmsPermission> permissions) {
        this.cmsUser = cmsUser;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return permissions.stream()
                .filter(permission -> permission.getValue() != null)
                //抽取全限值集合
                .map(permission -> new SimpleGrantedAuthority(permission.getValue()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return cmsUser.getPassword();
    }

    @Override
    public String getUsername() {
        return cmsUser.getNumber();
    }

    //当前账号是否已经过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //当前账号是否被锁
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //当前账号证书（密码）是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return cmsUser.getIsEnable();
    }
}
